#include "engine/EngineTest.h"
#include "image/ImageLoader.h"
#include "image/PixelImage.h"
#include "image/QImageFactory.h"
#include "image/QImageLoader.h"
#include "image/QPixelImage.h"
#include "text/htmltext.h"
#include "text/organizedtextcontext.h"
#include "text/htmlrenderer.h"
#include "text/qhtmlrenderer.h"
#include "text/chinesestrategy.h"

EngineTest::EngineTest(QApplication * application):
app(application)
{
	lSetInt = new QLabel(tr("Set Int"));
	lAddModule = new QLabel(tr("Add Module"));
	lGetNew = new QLabel(tr("Get New"));
	eSetInt = new QLineEdit;
	eAddModule = new QLineEdit;
	eGetNew = new QLineEdit;
	eGetInt = new QLineEdit;
	eGetModule = new QLineEdit;
	eFree = new QLineEdit;
	bGetInt = new QPushButton(tr("GetInt"));
	bGetModule = new QPushButton(tr("GetModule"));
	bFree = new QPushButton(tr("Free"));

	hBox0 = new QHBoxLayout;
	hBox0->addWidget(lSetInt);
	hBox0->addWidget(eSetInt);
	hBox0->addWidget(bGetInt);
	hBox0->addWidget(eGetInt);

	hBox1 = new QHBoxLayout;
	hBox1->addWidget(lAddModule);
	hBox1->addWidget(eAddModule);
	hBox1->addWidget(bGetModule);
	hBox1->addWidget(eGetModule);

	hBox2 = new QHBoxLayout;
	hBox2->addWidget(lGetNew);
	hBox2->addWidget(eGetNew);
	hBox2->addWidget(bFree);
	hBox2->addWidget(eFree);

        QImageFactory * imageFactory = (QImageFactory *)Singleton::getInstance(QImageFactory::getClassName());
	PixelImage * pixelImage = imageFactory->createPixelImage();
	image = 0;
        QImageLoader * imageLoader = (QImageLoader *)imageFactory->createImageLoader();
	if( imageLoader->loadImage("test.jpg") ){
		imageLoader->bindImage(pixelImage);
		image = ((QPixelImage *)pixelImage)->getLabel();
                pixmap = imageLoader->getPixmap();
	}

        text = new HtmlText();
        text->setText("Appends the first num characters of source to destination, plus a terminating null-character. If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.");
        OrganizedTextContext * context = new OrganizedTextContext;
        text->setContext(context);
        context->setLanguage(new ChineseStrategy);
        renderer = new QHtmlRenderer();
        renderer->setPixmap(pixmap);
        text->setRenderer(renderer);
        textDialog = new TextDialog(text);
        context->setFormat(20, 3);
        //renderer->render(text);

	vBox0 = new QVBoxLayout;
	vBox0->addLayout(hBox0);
	vBox0->addLayout(hBox1);
	vBox0->addLayout(hBox2);
        //if( image )
        //	vBox0->addWidget(image);

        setWindowTitle(tr("GCX Test"));
        this->setFixedWidth(pixmap->width());
        this->setFixedHeight(pixmap->height());
        setLayout(vBox0);

	connect(bGetInt, SIGNAL(clicked()), this, SLOT(onGetInt()));
	connect(bGetModule, SIGNAL(clicked()), this, SLOT(onGetModule()));
	connect(bFree, SIGNAL(clicked()), this, SLOT(onFree()));
}

EngineTest::~EngineTest(void)
{
    delete renderer;
    delete pixmap;
}

void EngineTest::paintEvent(QPaintEvent *){
    QPainter painter(this);
    painter.setBackgroundMode(Qt::TransparentMode);
    painter.drawPixmap(0, 0, *pixmap);
    renderer->render(textDialog, &painter, 20, 20, 40);
}

void EngineTest::onGetInt(){
        FiniteStateMachine *fsm = (FiniteStateMachine *)Singleton::getInstance(FiniteStateMachine::getClassName());
	char name[] = "test";
	fsm->setInt(name, eSetInt->text().toInt());
	QString str;
	str.setNum(fsm->getInt(name));
	eGetInt->setText(str);

        textDialog->next();
        this->update();
}

void EngineTest::onGetModule(){
	Module * module = new Module(eAddModule->text().toInt());
        GameEngine * engine = (GameEngine *)Singleton::getInstance(GameEngine::getClassName());
	engine->addModule(module);	
	int key = module->getKey();
	Module * module0 = engine->getModule(key);
	if(module0){
		QString str;
		str.setNum(module0->getKey());
		eGetModule->setText(str);
	}

        textDialog->active();
        this->update();
}

void EngineTest::onFree(){
	MemoryMag<int> * memo = new MemoryMag<int>(512);
        int * pValue = memo->alloc();
	*pValue = eGetNew->text().toInt();
	QString str;
	str.setNum(*pValue);
	eFree->setText(str);
	memo->free(pValue);
}

